package io.renren.modules.neo4j.service.impl;

import io.renren.modules.neo4j.entity.Edge;
import io.renren.modules.neo4j.entity.Node;
import io.renren.modules.neo4j.entity.node.course;
import io.renren.modules.neo4j.entity.node.knowledge;
import io.renren.modules.neo4j.entity.node.occupation;
import io.renren.modules.neo4j.respository.OccupationRespository;
import io.renren.modules.neo4j.service.OccupationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OccupationServiceImpl implements OccupationService {

    @Autowired
    OccupationRespository occupationRespository;

    @Override
    public Map findCourseByoccuoationId(Long id) {
        Optional<occupation> occupation = occupationRespository.findById(id, 2);
        if (occupation != null) {
            Set<course> courses = occupation.get().getCourse();
            List<Edge> edges = new ArrayList<>();
            List<Node> nodes = new ArrayList<>();

            for (course c : courses) {
                Node node = new Node();
                node.setId(c.id);
                node.setLabel(c.getName());
                nodes.add(node);
                if (c.getCourse() != null) {
                    for (course c1 : c.getCourse()) {
                        Edge edge = new Edge();
                        edge.setFrom(c.id);
                        edge.setTo(c1.id);
                        edges.add(edge);
                    }
                }
            }
            Map map = new HashMap();
            map.put("nodes", nodes);
            map.put("edges", edges);
            return map;
        }else{
            return null;
        }

    }

    @Override
    public Map getOccAndCourseAndKnowledge(Long id) {

        Optional<occupation> occupation = occupationRespository.findById(id,3);
        Set<course> courses=  occupation.get().getCourse();
        List<Edge> edges=new ArrayList<>();
        List<Node> nodes=new ArrayList<>();
        for(course c :courses){
            if(null != c.getKnowledge()){
                //获取课程下的包含关系的知识点
                List<knowledge> knowledges =  c.getKnowledge();

                //根据知识点的id找其下一层
                for(knowledge knowledge :knowledges){
                    Node node = new Node();
                    node.setId(knowledge.id);
                    node.setLabel(knowledge.getName());
                    nodes.add(node);
                    if(null != knowledge.getDownKnowledge() ){  //第二层的知识点
                        for(knowledge twoKnowledge:knowledge.getDownKnowledge()){
                            Edge edge = new Edge();
                            edge.setFrom(knowledge.id);
                            edge.setTo(twoKnowledge.id);
                            edge.setLabel(twoKnowledge.getName());
                            edges.add(edge);
                        }
                    }


                }
            }
        }
        Map map = new HashMap();
        map.put("nodes",nodes);
        map.put("edges",edges);
        return map;
    }
}
